package com.docMan.aspect;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.ws.rs.FormParam;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import com.docMan.dao.pojo.DocDirectory;
import com.docMan.service.DocDirectoryService;
import com.docMan.utils.CastUtil;
import com.docMan.utils.FinalConfig;

@Component("creatDirAspect")
@Aspect
@EnableAspectJAutoProxy
public class CreateDirAspect {
	private DocDirectoryService docDirectoryService;
	private HttpSession httpSession;

	@Autowired
	public void setHttpSession(HttpSession httpSession) {
		this.httpSession = httpSession;
	}

	@Autowired
	public void setDocDirectoryService(DocDirectoryService docDirectoryService) {
		this.docDirectoryService = docDirectoryService;
	}

	@Pointcut("execution(* com.docMan.service.impl.DocDirectoryServiceImpl.saveOrUpdateDirectory(..))")
	public void aspect() {

	}

	@Before(value = "aspect()")
	public void before(JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		System.out.println(CastUtil.castString(httpSession
				.getAttribute(FinalConfig.PersonalDirId)));
		if (args[0].getClass() == DocDirectory.class) {
			DocDirectory docDirectory = (DocDirectory) args[0];
			//判断是不是创建个人的网盘，是的话就放行
			if(docDirectory.getPersonalDisk()==FinalConfig.PersonalDiskDir){
				httpSession.setAttribute(FinalConfig.canCreatDir, true);
			}else{
				//对于文件夹信息的更新，同样放行
				if(null!=docDirectory.getId()&&docDirectory.getId()!=0){
					httpSession.setAttribute(FinalConfig.canCreatDir, true);
				}else {
					//在文件夹下创建
					if (null==docDirectory.getDiskId()&&docDirectory.getDirectoryId() != CastUtil.castInt(httpSession
							.getAttribute(FinalConfig.PersonalDirId))) {
						httpSession.setAttribute(FinalConfig.canCreatDir, false);
						DocDirectory parentDir=docDirectoryService.getById(docDirectory.getDirectoryId());
						String pathId=parentDir.getPath();
						//判断创建的文件父文件夹是不是在自己的个人网盘下
						if(pathId.contains(CastUtil.castString(httpSession.getAttribute(FinalConfig.PersonalDirId)))){
							httpSession.setAttribute(FinalConfig.canCreatDir, true);
						}
					}else {
						//在部门网盘下创建
						httpSession.setAttribute(FinalConfig.canCreatDir, true);
					}
				}
			}
		}
	}
}
